﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq;



namespace EncuentraWEB
{
       
    public partial class Formulario_web11 : System.Web.UI.Page
    {
        private ArqJsCls arq;
        private clsEmpresas objEmpresa;
        protected void Page_Load(object sender, EventArgs e)
        {
            var sUser = (string)Session["User"];
            if (sUser != "admin")
            {
                Response.Redirect("~\\default.aspx");
                return;
            }
            arq = new ArqJsCls();
            objEmpresa = new clsEmpresas();
            if (!this.IsPostBack)
           {
            try
            {
                Buscar();
                hidIndice.Value = "-1";
            }   
            catch (Exception ex)
            {
            }
           }
        }

        protected void grdEmpresas_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
          grdEmpresas.PageIndex = e.NewPageIndex;
          Buscar();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Buscar();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Modificar();
        }

        protected void rdbGVRow_CheckedChanged(object sender, System.EventArgs e)
        {
            //Clear the existing selected row 
            foreach (GridViewRow oldrow in grdEmpresas.Rows)
            {
                ((RadioButton)oldrow.FindControl("rdbGVRow")).Checked = false;
            }

            //Set the new selected row
            RadioButton rb = (RadioButton)sender;
            GridViewRow row = (GridViewRow)rb.NamingContainer;
            ((RadioButton)row.FindControl("rdbGVRow")).Checked = true;
        }

     

        protected void Button6_Click(object sender, EventArgs e)
        {
            Eliminar();
        }

        #region Metodos Privados
        private void Buscar()
        {

            DataTable dt = objEmpresa.buscar(txtEmpresa.Text, txtCuit.Text);
            int b = dt.Rows.Count;
            int a = b %= 10;
            if (a != 0)
            {
                for (int i = 1; i <= (10 - a); i++)
                {
                    dt.Rows.Add();
                }
            }

            try
            {
                this.grdEmpresas.DataSource = dt;
                this.grdEmpresas.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        private void Agregar()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            arq.Funciones(this, "ShowEditModal('frmamEmpresa.aspx?id=I&id1=0')");
            ModalPop.TargetControlID = "btnImgAgregar";
            ModalPop.Show();
            ModalPop.TargetControlID = "btnFix";

        }
        private void Modificar()
        {
            if (Convert.ToInt32(hidIndice.Value) >= 0)
            {
                var id = grdEmpresas.Rows[Convert.ToInt32(hidIndice.Value)].Cells[1].Text;
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                arq.Funciones(this, "ShowEditModal('frmamEmpresa.aspx?id=M&id1=" + id + "')");
                ModalPop.TargetControlID = "btnImgAgregar";
                ModalPop.Show();
                ModalPop.TargetControlID = "btnFix";
            }
            else
            {
                arq.Msg(this, "No se ha seleccionado ninguna fila");
            }
        }
        private void Eliminar()
        {
            if (Convert.ToInt32(hidIndice.Value) >= 0)
            {
             //   if (hidResultado.Value != "false")
               // {
                    var id = Convert.ToInt32(grdEmpresas.Rows[Convert.ToInt32(hidIndice.Value)].Cells[1].Text);
                    objEmpresa.Eliminar(id);
                    Buscar();
           //         arq.Funciones(this, "cambiar()");
                //}
               // else
                //{
                  //  var id = grdEmpresas.Rows[Convert.ToInt32(hidIndice.Value)].Cells[1].Text;
                  //  arq.Funciones(this, "eliminar()");
                //}
            }
            else
            {
                arq.Msg(this, "No se ha seleccionado ninguna fila");
            }
        }
        #endregion

        protected void btnImgFiltrar_Click(object sender, ImageClickEventArgs e)
        {
            Buscar();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Agregar();
        }

        protected void btnImgEditar_Click(object sender, ImageClickEventArgs e)
        {
            Modificar();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            Modificar();
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            Eliminar();
        }
    }
}
